package com.example.dps;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.downpayment.dps.R;

public class MFAActivity extends Activity{
	
	WebHelper webHelper;
	DBHelper dbHelper;
	AccountDBHelper accountDBHelper;
	
	/** ID of bank **/
	int contentServiceID = 0;
	
	/** number of MFA fields **/
	int count;
	
	/** the bank item id **/
	int itemID;
	
	String userContext;
	
	/** the questions asked **/
	String[] questions;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mfa_security_question_layout);
		
		
		this.webHelper = new WebHelper(this);
		this.accountDBHelper = new AccountDBHelper(this);
		this.dbHelper = new DBHelper(this);
		
		addButtonClickListeners();
		
		LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		LinearLayout root = (LinearLayout) findViewById(R.id.backgroundLayout);
		
		JSONObject json;

		try {
			
			json = new JSONObject(this.getIntent().getExtras().getString("json"));
			
			String mfaType = json.getString("mfaType");
			
			
			
			if(mfaType.equals("SingleQuestionSingleAnswer")){
				
				handleSingleQuestionSingleAnswer(json);
				
			}

			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

		
		
		
	}
	
	public void handleSingleQuestionSingleAnswer(JSONObject json) throws JSONException{
		
		LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		LinearLayout root = (LinearLayout) findViewById(R.id.backgroundLayout);
		
		
		count = json.getInt("count");
		itemID = json.getInt("itemID");
		userContext = json.getString("userContext");
		contentServiceID = json.getInt("contentServiceID");
		
		Log.e("MFA", "single question CID = " + contentServiceID);
		
		
		questions = new String[count];
		
		for(int i = 0; i < count; i++){
			
			questions[i] = json.getString(String.valueOf(i));
			
			View questionView = inflater.inflate(R.layout.mfa_security_question_item, null);
			
			TextView questionText = (TextView)questionView.findViewById(R.id.questionText);
			questionText.setText(questions[i]);
			
			questionView.setId(i+1);
			
			root.addView(questionView);
		}
		
		
		
		
		
	}
	
	
	public void addButtonClickListeners(){
		
		ImageButton continueButton = (ImageButton)findViewById(R.id.continueButton);
		
		continueButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				
				JSONObject json = new JSONObject();
				
				try {
					
					//Log.e("MFA", "single question CID = " + contentServiceID);
					
					
					json.put("count", count);
					json.put("itemID", itemID);
					json.put("userContext", userContext);
					json.put("contentServiceID", contentServiceID);
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				for(int i = 0; i < count; i++){
					
					
					View questionView = (View) findViewById(i+1);
					
					EditText answer = (EditText) questionView.findViewById(R.id.questionAnswer);
					
					String answerText = answer.getText().toString();
					
					try {
						json.put("question"+i, questions[i]);
						json.put("answer"+i, answerText);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
				
				MFAActivity.this.webHelper.answerMFA(json);
				
				
			}
			
		});
		
		
		
	}
	

}
